import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
import { UnitsProvider } from '../../providers/units/units';
/**
 * Generated class for the AssistantfixedreturnPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-assistantfixedreturn',
  templateUrl: 'assistantfixedreturn.html',
})
export class AssistantfixedreturnPage {
  /**
	 * 输入界面控制
	 */
	inputViewModel = {
		applyapplymember: '', //申请人
    applytime: '', //申请日期
    applycompany: new Object(), //申请公司
    applydepartment: new Object(), //申请部门
    applychangenote:'',//申请理由
    applyassetsfixed: new Object(), //资产数据
    applyassetsnums: '', //现归还
		//
		applynote: '', //备注
		applyuploadfile: '', //附件
		applynownode: '', //当前节点
		applynowmember: '', //当前处理人
		applynextnode: '', //下一步审批节点
		applynextman: '', //下一步审批人
	}

	constructor(
		public navCtrl: NavController,
		public navParams: NavParams,
		public http: HttpProvider,
		public storage: StorageProvider,
		public dialog: DialogProvider,
		public units: UnitsProvider) {
	}

  ionViewDidLoad() {
    console.log('ionViewDidLoad AssistantfixedreturnPage');
    this.ionicLoad();
  }

  //当前节点
  ionicLoad() {
    //控制显示
    this.getUserInfo();
    this.inputViewModel.applytime = this.units.getCurrentDate(new Date().getTime());
    this.inputViewModel.applynownode = '发起申请'; //当前节点
    this.inputViewModel.applynextnode = '部门经理审批'; //当前节点
  }
  //获取本用户信息
  getUserInfo() {
    this.storage.getStorage("user").then(val => {
      //请求方式如下
      let params = {
        user_id: val['user_id']
      };
      this.http.postJson('user', 'douserinfo.do', params).then(res => {
        if (res['success'] == true) {
          let info = res['data'];
          //请求成功
          this.inputViewModel.applyapplymember = info['name'];
          this.inputViewModel.applynowmember = info['name'];
        } else {
          this.dialog.showToast("top", res['msg']);
        }
      });
    });
  }
  //初始化选择公司
  applycompanyselect() {
    //共通弹出
    let data = new Object();
    data['selectitem'] = this.inputViewModel.applycompany;
    data['multiselect'] = false; //false 单选 true 多选
    let opts = new Object();
    this.dialog.showModal("ModalcompanyselectPage", data, opts).then(success => {
      //界面关闭的时候
      if (success) {
        let result = success['result'];
        this.inputViewModel.applycompany = result[0];
      }
    });
  }
  //初始化选择部门
  applydepartselect() {
    //共通弹出
    let data = new Object();
    data['selectitem'] = this.inputViewModel.applydepartment;
    data['multiselect'] = false; //false 单选 true 多选
    let opts = new Object();
    this.dialog.showModal("ModaldepartselectPage", data, opts).then(success => {
      //界面关闭的时候
      if (success) {
        let result = success['result'];
        this.inputViewModel.applydepartment = result[0];
      }
    });
  }
  //初始化选择用户
  applyapplymemberselect() {
    //共通弹出
    let data = new Object();
    data['selectitem'] = this.inputViewModel.applynextman;
    data['multiselect'] = false; //false 单选 true 多选
    let opts = new Object();
    this.dialog.showModal("ModaluserselectPage", data, opts).then(success => {
      //界面关闭的时候
      if (success) {
        let result = success['result'];
        this.inputViewModel.applynextman = result[0];
      }
    });
  }
  //固定资产选择
  applyfixedselect() {
    //共通弹出
    let data = new Object();
    data['selectitem'] = this.inputViewModel.applyassetsfixed;
    data['multiselect'] = false; //false 单选 true 多选
    data['status'] = 2;//归还
    let opts = new Object();
    this.dialog.showModal("ModalsfixedselectPage", data, opts).then(success => {
      //界面关闭的时候
      if (success) {
        let result = success['result'];
        this.inputViewModel.applyassetsfixed = result[0];
      }
    });
  }
  //提交数据
  applyapplydone() {
    if (this.checkInput()) {
      //初始化
      this.storage.getStorage("user").then(val => {
        //请求方式如下
        //请求方式如下
        let params = {
          'user_id': val['user_id'],
          'company': this.inputViewModel.applycompany['id'],
          'drafterDept.id': this.inputViewModel.applydepartment['id'],
          'applyReason': this.inputViewModel.applychangenote,
          'assets.id': this.inputViewModel.applyassetsfixed['id'],
          'remarks': this.inputViewModel.applynote,
          'auditPersonId': this.inputViewModel.applynextman['id'].substring(2),
          'nowReturn': this.inputViewModel.applyassetsnums,
        };
        //收文传阅列表
        this.http.postJson('oa', 'assetsReturnApply/insert.do', params).then(res => {
          if (res['success']) {
            this.dialog.showAlert("提示","数据提交成功").then(()=>{
              this.navCtrl.pop();
            });
          } else {
            this.dialog.showAlert("提示",res["msg"]).then(()=>{
            });
          }
        });
      });
    }
  }

  //检查输入
  checkInput() {
    if (this.inputViewModel.applycompany['id'] == undefined) {
      this.dialog.showToast("top", "请选择申请公司"); return false;
    }
    if (this.inputViewModel.applydepartment['id'] == undefined) {
      this.dialog.showToast("top", "请选择申请部门"); return false;
    }
    if (this.inputViewModel.applychangenote === "") {
      this.dialog.showToast("top", "请输入申请理由"); return false;
    }
    if (this.inputViewModel.applyassetsfixed['id'] == undefined) {
      this.dialog.showToast("top", "请选择资产"); return false;
    }
    if (this.inputViewModel.applyassetsnums == "") {
      this.dialog.showToast("top", "请输入现归还"); return false;
    }
    if (this.inputViewModel.applynote == "") {
      this.dialog.showToast("top", "请输入备注"); return false;
    }
    if (this.inputViewModel.applynextman['id'] == "") {
      this.dialog.showToast("top", "请选择下一步审批人"); return false;
    }
    return true;
  }
}

